package codeRandomThoughts.SortAlgorithm;

import java.util.Arrays;

public class SelectionSort {
    private static void selectionSort(int[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            //我们只需要对len - 1个元素排序即可,最后一个元素肯定在该在的位置上
            //i表示每轮选择最小元素要交换到的目标索引
            int minIndex = i;
            for (int j = minIndex + 1; j < a.length; j++) {
                if (a[j] < a[minIndex]) {
                    minIndex = j;
                }
            }
            int tem = a[minIndex];
            a[minIndex] = a[i];
            a[i] = tem;
        }
    }

    public static void main(String[] args) {
        int[] a = {4, 3, 5, 2, 1, 9, 7};
        selectionSort(a);
        System.out.println(Arrays.toString(a));
    }
}
